import defaultSettings from './defaultSettings'; // https://umijs.org/config/

import slash from 'slash2';
import themePluginConfig from './themePluginConfig';
const { pwa } = defaultSettings; // preview.pro.ant.design only do not use in your production ;
// preview.pro.ant.design 专用环境变量，请不要在你的项目中使用它。
const { ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION } = process.env;
const isAntDesignProPreview = ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site';
const plugins = [
  [
    'umi-plugin-react',
    {
      antd: true,
      dva: {
        hmr: true,
      },
      locale: {
        // default false
        enable: true,
        // default zh-CN
        default: 'zh-CN',
        // default true, when it is true, will use `navigator.language` overwrite default
        baseNavigator: true,
      },
      dynamicImport: {
        loadingComponent: './components/PageLoading/index',
        webpackChunkName: true,
        level: 3,
      },
      pwa: pwa
        ? {
          workboxPluginMode: 'InjectManifest',
          workboxOptions: {
            importWorkboxFrom: 'local',
          },
        }
        : false,
      // default close dll, because issue https://github.com/ant-design/ant-design-pro/issues/4665
      // dll features https://webpack.js.org/plugins/dll-plugin/
      // dll: {
      //   include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
      //   exclude: ['@babel/runtime', 'netlify-lambda'],
      // },
    },
  ],
  [
    'umi-plugin-pro-block',
    {
      moveMock: false,
      moveService: false,
      modifyRequest: true,
      autoAddMenu: true,
    },
  ],
  ['umi-plugin-antd-theme', themePluginConfig]
];

if (isAntDesignProPreview) {
  // 针对 preview.pro.ant.design 的 GA 统计代码
  plugins.push([
    'umi-plugin-ga',
    {
      code: 'UA-72788897-6',
    },
  ]);
  // plugins.push(['umi-plugin-antd-theme', themePluginConfig]);
}

export default {
  plugins,
  hash: true,
  targets: {
    ie: 11,
  },
  // umi routes: https://umijs.org/zh/guide/router.html
  routes: [
    {
      path: '/user',
      component: '../layouts/UserLayout',
      routes: [
        {
          name: 'login',
          path: '/user/login',
          component: './User/Login',
        },
        {
          redirect: '/404'
        },
      ],
    },
    {
      path: './404',
      component: './Exception/404',
    },
    {
      path: '/resetpassword',
      component: './Exception/ResetPassword',
    },
    {
      path: '/',
      component: '../layouts/BasicLayout',
      Routes: ['src/pages/Authorized'],
      authority: ['admin'],
      routes: [
        {
          path: '/',
          hidden: true,
          component: '../layouts/BlankLayout',
        },
        {
          path: '/dutyNotes',
          name: '突发事件',
          component: './DutyNotes'
        },
        {
          path: '/dutyNotesNew',
          name: '值守保障',
          component: './DutyNotesNew',
          routes: [
            {
              path: 'dutyGuarantee',
              name: '值守保障',
              component: './DutyNotesNew/DutyGuarantee',
            },
            {
              path: 'dutyRandomCheck',
              name: '抽查情况',
              component: './DutyNotesNew/DutyRandomCheck',
            },
            {
              component: './Exception/404'
            }
          ]
        },
        {
          path: '/emergency',
          name: '突发事件',
          component: './Emergency'
        },
        {
          path: '/duty',
          name: '值班排班',
          component: './Duty',
          routes: [
            {
              path: 'dutyArrange',
              name: '值班管理',
              component: './Duty/DutyArrange'
            },
            {
              path: 'shiftArrange',
              name: '排班管理',
              component: './duty/dutyShift'
            },
            {
              path: 'title',
              name: '头衔设置',
              component: './duty/title'
            },
            {
              path: 'shift',
              name: '班次设置',
              component: './duty/shift'
            },
            {
              component: './Exception/404'
            }
          ]
        },
        // 通讯录-值班排班（合二为一）
        // {
        //   path: '/dutyShiftArrangeNew',
        //   name: '突发应对',
        //   component: './DutyShiftArrangeNew'
        // },
        {
          path: '/addressBook',
          name: '通讯录',
          component: './Staff'
        },
        {
          path: '/staff',
          name: '人员管理',
          component: './Staff'
        },
        {
          path: '/organization',
          name: '机构管理',
          component: './Organization',
          routes: [
            {
              path: 'unit',
              name: '单位管理',
              component: './Organization/Unit'
            },
            {
              path: 'department',
              name: '部门管理',
              component: './Organization/Department'
            },
            {
              path: 'jobTitle',
              name: '职务管理',
              component: './Organization/JobTitle'
            },
            {
              component: './Exception/404'
            }
          ]
        },
        {
          path: '/sysmanage',
          name: '系统管理',
          component: './SysManage',
          routes: [
            {
              path: 'role',
              name: '权限管理',
              component: './SysManage/Role',
              authority: ['role']
            },
            {
              path: 'menu',
              name: '资源管理',
              component: './SysManage/Menu',
              authority: ['menu']
            },
            {
              component: './Exception/404'
            }
          ]
        },
        {
          component: './Exception/404'
        }
      ]
    }
  ],
  // Theme for antd: https://ant.design/docs/react/customize-theme-cn
  theme: {
    // ...darkTheme,
  },
  define: {
    ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION:
      ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION || '', // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量，请不要在你的项目中使用它。
  },
  ignoreMomentLocale: true,
  lessLoaderOptions: {
    javascriptEnabled: true,
  },
  disableRedirectHoist: true,
  cssLoaderOptions: {
    modules: true,
    getLocalIdent: (context, _, localName) => {
      if (
        context.resourcePath.includes('node_modules') ||
        context.resourcePath.includes('ant.design.pro.less') ||
        context.resourcePath.includes('global.less')
      ) {
        return localName;
      }

      const match = context.resourcePath.match(/src(.*)/);

      if (match && match[1]) {
        const antdProPath = match[1].replace('.less', '');
        const arr = slash(antdProPath)
          .split('/')
          .map(a => a.replace(/([A-Z])/g, '-$1'))
          .map(a => a.toLowerCase());
        return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-');
      }

      return localName;
    },
  },
  manifest: {
    basePath: '/',
  },
  // chainWebpack: webpackPlugin,
  proxy: {
    // '/server/api/': {
    //   target: 'https://preview.pro.ant.design/',
    //   changeOrigin: true,
    //   pathRewrite: { '^/server': '' },
    // },
    // '/visdata/rest': {
    //   'target': 'http://10.207.204.38:8080',
    //   'changeOrigin': true,
    //   'ws': true
    // }
  },
};
